package com.lzh.boot.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzh.boot.pojo.BaseDict;
import com.lzh.boot.pojo.Customer;
import com.lzh.boot.pojo.QueryVo;
import com.lzh.boot.service.BaseDictService;
import com.lzh.boot.service.CustomerService;
import com.lzh.boot.util.Page;

/**
 * 客户管理
 * @author Administrator
 *
 */
@Controller
public class CustomerController {
	@Autowired
	private BaseDictService baseDictService;
	@Autowired
	private CustomerService customerService;
	@Value("${customer.source.cood}")
	private String custSourceCode;
	@Value("${customer.industory.code}")
	private String custindustoryCode;
	@Value("${customer.level.code}")
	private String custlevelCode;
	@RequestMapping("/customer/list")
	public String showCostomerList(QueryVo queryVo,Model model) throws Exception{
		/*//get乱码问题:
		if(StringUtils.isNotBlank(queryVo.getCustName())){
			queryVo.setCustName(new String(queryVo.getCustName().getBytes("iso8859-1"),"GBK"));
		}*/
		//初始化客户来源
		List<BaseDict> sourceList = baseDictService.getDictListByTypeCode(custSourceCode);
		//所属行业
		List<BaseDict> industoryList = baseDictService.getDictListByTypeCode(custindustoryCode);
		//客户级别
		List<BaseDict> levelList = baseDictService.getDictListByTypeCode(custlevelCode);

		//把字典信息传递给JSP
		model.addAttribute("fromType",sourceList);
		model.addAttribute("industryType", industoryList);
		model.addAttribute("levelType", levelList);
		//根据查询条件客户列表
		Page<Customer> page = customerService.getCustomerList(queryVo);
		//把客户列表传递给页面
		model.addAttribute("page",page);
		//参数回显
		model.addAttribute("custName",queryVo.getCustName());
		model.addAttribute("custSource",queryVo.getCustSource());
		model.addAttribute("custIndustry",queryVo.getCustIndustory());
		model.addAttribute("custLevel",queryVo.getCustLevel());

		return "customer";
	}
	
	
	@RequestMapping("/customer/edit")
	@ResponseBody//pojp就会转化成json
	public Customer getCustomerById(Long id) {
		Customer customer = customerService.getCustomerById(id);
		return customer;
	}
	@RequestMapping(value="customer/update",method=RequestMethod.POST)
	@ResponseBody//直接响应浏览器
	public String updateCustomer(Customer customer){
		customerService.updateCustomer(customer);
		
		return "OK";
		
	}
	
	@RequestMapping("/customer/delete")
	@ResponseBody
	public String deleteCustomer(Long id){
		customerService.deleteCustomer(id);
		return "OK";
	}
	
}
